import { Method, Type } from 'src/common/enum';
import { Admin } from './admin.entity';
import {
  BaseEntity,
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  JoinTable,
  ManyToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity()
export class Permission extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true, comment: '名称' })
  name: string;

  @Column({ comment: '请求方式' })
  method: Method;

  @Column({ comment: '请求路径' })
  path: string;

  @Column({ comment: '排序', default: 0 })
  sort: number;

  @Column({
    comment: '是否菜单 0非菜单 1 菜单',
    type: 'tinyint',
    default: Type.Api,
  })
  type: Type;

  @Column({ comment: '页面组件名', nullable: true })
  componentName: string;

  @CreateDateColumn()
  createTime: Date;
  @UpdateDateColumn()
  updateTime: Date;
  @DeleteDateColumn({ nullable: true })
  deleteTime: Date;

  @ManyToMany(() => Admin, (admin) => admin.permissions)
  @JoinTable()
  admins: Admin[];
}
